package com.inossem.allotin.bl;


import android.content.Context;
import android.os.Environment;


import com.inossem.allotin.bl.delegate.BLDelegate;
import com.inossem.allotin.utils.Utils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

/**
 * Created by GUO on 2017/7/7.
 */

public class DownloadBl {

    private Context mContext;
    private BLDelegate.DownLoadDelegate uploadDelegate;

    public DownloadBl(Context context) {
        mContext = context;
    }

    public void setDownloadDelegate(BLDelegate.DownLoadDelegate delegate) {
        this.uploadDelegate = delegate;
    }

    public void downloadFile(String url, String fileName) {

        RequestParams params = new RequestParams(url);
        //自定义保存路径，Environment.getExternalStorageDirectory()：SD卡的根目录
        params.setSaveFilePath(Utils.getFileDir(mContext) + File.separator + fileName);
        //自动为文件命名
//        params.setAutoRename(true);
        x.http().get(params, new Callback.CommonCallback<File>() {
            @Override
            public void onSuccess(File result) {
                uploadDelegate.onSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                uploadDelegate.onError(ex, isOnCallback);
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }
}
